package com.eqdd.yiqidian.ui.gongneng.daka;

import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.bigkoo.pickerview.TimePickerView;
import com.eqdd.yiqidian.DakaListActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.bean.rebuild.ClockBean;
import com.eqdd.yiqidian.bean.rebuild.ClockHistoryListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DakaListActivity extends BaseActivity {
    private DakaListActivityCustom dataBinding;
    private SimpleDateFormat sf;
    private SlimAdapterEx<ClockHistoryListResultBean> slimAdapterEx;
    private TimePickerView time;
    private Calendar now;
    private Calendar from;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_daka_list);
        initTopRightText("前月记录", v -> {
            if (time == null) {
                time = new TimePickerView.Builder(this, (date, v1) -> {
                    refresh(sf.format(date));
                })
                        .setOutSideCancelable(true)
                        .setType(new boolean[]{true, true, false, false, false, false})
                        .setDate(Calendar.getInstance())
                        .setRangDate(from, now)
                        .build();
            }
            time.show();
        });
        now = Calendar.getInstance();
        now.setTime(new Date());
        from = Calendar.getInstance();
        from.set(2017, 1, 1);
        dataBinding.includeItemTitle.tvLeft.setText("日期");
        dataBinding.includeItemTitle.tvCenter.setText("说明");
        dataBinding.includeItemTitle.tvRight.setText("状态");

    }

    @Override
    public void initData() {

        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_29, new SlimInjector<ClockHistoryListResultBean>() {
            @Override
            public void onInject(ClockHistoryListResultBean data, IViewInjector injector) {
                int type = 0;// 1,迟到或早退 2 漏打卡 0正常 有优先级
                if (data.getList() == null || data.getList().size() == 0) {
                    type = 2;
                } else {
                    for (ClockBean clockBean : data.getList()) {
                        if (clockBean.getStatus() == 1 || clockBean.getStatus() == 2) {
                            type = 1;
                            break;
                        }
                        if (clockBean.getStatus() == -2) {
                            type = 2;
                        }
                    }
                }
                injector.text(R.id.tv_left_content, data.getDate())
                        .text(R.id.tv_right_content, type == 1 ? "迟到或早退" : (type == 2 ? "" : "正常"))
                        .textColor(R.id.tv_right_content, (type == 1 || type == 2) ? Color.parseColor("#ff0000") : Color.parseColor("#000000"));
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {

                    Bundle bundle = new Bundle();
                    bundle.putSerializable("bean", slimAdapterEx.getDataItem(position));
                    startActivity(DakaDetailActivity.class, bundle);
                });

    }

    @Override
    public void setView() {

        sf = new SimpleDateFormat("yyyy-MM-dd");
        String title = sf.format(new Date());
        initTopTitleBar(View.VISIBLE, title.substring(0, 7));
        refresh(title);
    }

    private void refresh(String title) {
        OkGo.<HttpResult<List<ClockHistoryListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.CLOCK_HISTORY_LIST)
                .params("userGuid", user.getGuid())
                .params("Month", title)
                .execute(new DialogCallBack<HttpResult<List<ClockHistoryListResultBean>>>(DakaListActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<ClockHistoryListResultBean>>> response) {
                        HttpResult<List<ClockHistoryListResultBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            dataBinding.includeTitle.tvTitle.setText(title.substring(0, 7));
                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
